/*
Element.prototype.hasClassName = function(name) {
	return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(this.className);
};

Element.prototype.addClassName = function(name) {
	if (!this.hasClassName(name)) {
		this.className = this.className ? [this.className, name].join(' ') : name;
	}
};

Element.prototype.removeClassName = function(name) {
	if (this.hasClassName(name)) {
		var c = this.className;
		this.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), "");
	}
};
//*/

// Function.prototype.bind polyfill
if ( !Function.prototype.bind ) {
	Function.prototype.bind = function( obj ) {
		if(typeof this !== 'function') // closest thing possible to the ECMAScript 5 internal IsCallable function
			throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
		var slice = [].slice,
			args = slice.call(arguments, 1),
			self = this,
			nop = function () {},
			bound = function () {
				return self.apply( this instanceof nop ? this : ( obj || {} ),
				args.concat( slice.call(arguments) ) );   
			};
		bound.prototype = this.prototype;
		return bound;
	};
}

var _hasClassName = function(obj, name) {
	return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(obj.className);
};

var _addClassName = function(obj,name) {
	if (!_hasClassName(obj,name)) {
		obj.className = obj.className ? [obj.className, name].join(' ') : name;
	}
};

var _removeClassName = (function(){
	var c;//make func more garbage friendly (no more recursion or thread compatible)
	return function(obj,name) {
		if (_hasClassName(obj,name)) {
			c = obj.className;
			obj.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), "");
		}
	};
})();

var _absPos = (function(){
	var curleft, curtop;//make func more garbage friendly (no more recursion or thread compatible)
	return function(obj) {
		curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return {x:curleft, y:curtop};
	}
})();

var _log;
var _root;
var logpush = (function(){
	var loghide;
	return function (msg){
		_log.innerHTML='<li>'+msg+'</li>'+_log.innerHTML;
		_addClassName(_log, 'logshow');
		if (loghide) clearTimeout(loghide);
		loghide = setTimeout(function(){
			_removeClassName(_log, 'logshow');
			loghide = null;
		},1500);
	}
})();

var logclear = function(){
	_log.innerHTML = '';
}

var _global = this;

var _registerClass,_inherit;
(function(){
	var classes = {};
	_registerClass = function(className, cons){
		cons.prototype._class = className;
		classes[className] = cons;
	}
	_inherit = function(childname, child, parent){
		_registerClass(childname, child);
		classes[childname].prototype.__proto__ = parent.prototype;
	}
})();

var _browser ={
	is_mobile: null,
	is_desktop: null,
	is_ios: null,
	is_android: null,
	is_chrome:null,
	is_safari:null,
	is_firefox:null,
	is_webkit:null,
	detect: function(){
		var uagent = navigator.userAgent.toLowerCase();
		with(this){
			is_ios =
				uagent.search("iphone")>-1 ||
				uagent.search("ipod")>-1 ||
				uagent.search("ipad")>-1;
			is_android =
				uagent.search("android")>-1;
			is_mobile = is_ios || is_android;
			is_desktop = !is_mobile;
			is_chrome = uagent.search("chrome")>-1;
			is_safari = (!is_chrome) && uagent.search("safari")>-1;
			is_webkit = is_chrome || is_safari;
			is_firefox = uagent.search("firefox")>-1;
			logpush(uagent);
		}
	},
	simulateMobile: function(){
		with(this){
			is_android = true;
			is_mobile = true;
		}
		void(function(scriptSrc,config){
			var script=document.createElement('script');
			script.src=scriptSrc;
			script.type='text/javascript';
			script.addEventListener('load',function(){
				if('phantomLimb'in window){
					phantomLimb.init(config)
				}else{
					console.error('Phantom Limb could not be loaded')
				}
			},false);
			document.head.appendChild(script)
		}
		(
			'./js/phantomLimb.js',
			{src:'./img/limb-black.png',lefty:false}
		));
	}
}

function _loadScripts(scriptSrcs, readyFunc){
	var totalSrc = 0;
	var count = 0;
	scriptSrcs.forEach(function(){
		count++;
	});
	var ready = function(src){
		logpush('Finish script '+src);
		totalSrc++;
		if (totalSrc == count){
			readyFunc();
		}
	}
	scriptSrcs.forEach(function(src){
		(function(mysrc){
			logpush('Loading script '+mysrc);
			var head= document.getElementsByTagName('head')[0];
			var script= document.createElement('script');
			script.type= 'text/javascript';
			script.onload= function(){ready(mysrc)};
			script.src= mysrc;
			head.appendChild(script);
			script.onreadystatechange= function () {
				if (/*this.readyState == 'complete' ||*/ this.readyState == 'loaded'){
					//loadedScript[mysrc] = true;
					ready(mysrc);
				}
			}
		})(src);
	});
}

var onReady;


rootload = function(){
	_root = document.getElementById('_root');
	_log = document.getElementById('log');
	_browser.detect();
	//_browser.simulateMobile();
	
	false?//this is for the obfuscated version!
		onReady():
		//load scripts
		_loadScripts(
			['js/PropertyList.js',
			"js/Storage.js",
			"js/DragDrop.js"],
			function(){_loadScripts(
				["js/DragButton.js"],
				function(){_loadScripts(
					["js/WebCanvas.js",
					"js/DockPanel.js"],
					onReady
				)}
			)}
		);
}

var _rootHolder;
var _webcanvas;
var _propList;
onReady = function(){
	
	UI.makePannable(_log);
	
	/*	
	var ff=function(){return 'abc';}
	logpush(ff.toString());
	*/
	
	/*
	_root.style.overflow = 'hidden';
	if (_browser.is_mobile && _browser.is_safari){
		document.addEventListener('touchstart',function(e){
			e.preventDefault();
			return false;
		},false);
	}*/
	
	var palette = new DockPanel(document.getElementById('palette'));
	var canvas = new DockPanel(document.getElementById('canvas'));
	//canvas.setPosSize('0','0','100%','100%');
	_rootHolder = new DockHolder(_root);
	_rootHolder.addPanel(canvas, 0, 0);
	var asset = new DockPanel(document.getElementById('asset'));
	var property = new DockPanel(document.getElementById('property'));
	var logic = new DockPanel(document.getElementById('logic'));

	canvas.dropPanel(palette,0,0);
	canvas.holder.setSpliter(20);
	palette.dropPanel(property,1,1);
	palette.holder.setSpliter(40);
	canvas.dropPanel(asset,1,1);
	canvas.holder.setSpliter(70);
	canvas.dropPanel(logic,0,1);
	canvas.holder.setSpliter(70);

	_webcanvas = new WebCanvas(document.getElementById('webcanvas'));

	Storage.registerStorage('DockHolder', DockHolder.saveFunc, DockHolder.loadFunc);
	
	var _button1 = new DragButton(document.getElementById('pal_button'), WebButton);
	var _button2 = new DragButton(document.getElementById('pal_textbox'), WebTextBox);
	
	/*_button1.elem.style['width'] = '150px';
	_button1.elem.style.mozBorderRadius = '5pt';
	_button1.elem.style['borderRadius'] = '5pt';*/
	
	_propList = new PropertyList(property.content);
	
	if (_browser.is_mobile){
		document.getElementById('undobtn').addEventListener('touchend',function(){
			Revision.undo();
		},false);
		document.getElementById('redobtn').addEventListener('touchend',function(){
			Revision.redo();
		},false);
	}else{
		document.getElementById('undobtn').addEventListener('click',function(){
			Revision.undo();
		},false);
		document.getElementById('redobtn').addEventListener('click',function(){
			Revision.redo();
		},false);		
	}
	
	/*//full screen for android, doesn't work
	if (_browser.is_android){
		var elem = document.createElement('div');
		elem.style.position = 'absolute';
		elem.style.top = '100%';
		elem.style.left = '0%';
		elem.style.height = '10px';//so that user can scroll down
		elem.style.width = '30%';
		_root.appendChild(elem);
		setTimeout(function() {
			window.scrollTo(0, 50);
			//_root.removeChild(elem);
		}, 3);
	}*/
}
